//
//  HotSecondViewController.m
//  HomePage
//
//  Created by dlios on 15-1-28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotSecondViewController.h"
//#import "HotThirdViewController.h"
#import "AFNetworking.h"
#import "HotSecondCell.h"
#import "HotSecond.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshBaseView.h"
#import "MJRefreshConst.h"
#import "UIScrollView+MJRefresh.h"
#import "UIScrollView+MJExtension.h"
#import "UIView+MJExtension.h"
#import "HotThirdViewController.h"
#import <SVProgressHUD.h>


@interface HotSecondViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tableview;



@end




@implementation HotSecondViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.tname;
    self.view.backgroundColor = [UIColor colorWithRed:225/255.0 green:237/255.0 blue:238/255.0 alpha:1];
   self.shu = 2;
    [self CreateTableViewShow];
    [self request];
    [self head];

}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)head
{
    [self.tableview addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableview addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.tableview headerBeginRefreshing];
    self.tableview.headerPullToRefreshText = @"下拉刷新";
    self.tableview.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableview.headerRefreshingText = @"正在刷新中,请稍后";
    
    self.tableview.footerPullToRefreshText = @"上拉加载更多数据";
    self.tableview.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.tableview.footerRefreshingText = @"正在加载中,不客气";

}

- (void)headerRereshing
{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableview reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableview headerEndRefreshing];
    });

    
}
//******************
- (void)footerRereshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableview reloadData];
        
        NSString *string = [NSString stringWithFormat:@"%ld",(long)self.shu];
        NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=all&condition=hot&device=android&page=%@&per_page=20&status=0&tag_name=%@",string,self.tname];
        NSString *strTrue = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        [manager GET:strTrue parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = responseObject;
            NSMutableArray *arry = [dic objectForKey:@"list"];
            for (NSDictionary *dicList in arry) {
                HotSecond *SecondHot = [[HotSecond alloc] init];
                [SecondHot setValuesForKeysWithDictionary:dicList];
                [self.arr addObject:SecondHot];
            }
            [self.tableview reloadData];
            [self.tableview footerEndRefreshing];
            self.shu += 1;
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           // NSLog(@"error: %@", error);
        }];
    });
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotThirdViewController *hotThirdVc = [[HotThirdViewController alloc] init];
    hotThirdVc.number = [[self.arr objectAtIndex:indexPath.row] id];
    
    HotSecond *second = [self.arr objectAtIndex:indexPath.row];
    hotThirdVc.titleStr = second.title;
    
    [self.navigationController pushViewController:hotThirdVc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 6.3;
}
- (void)request
{
    self.arr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%@",self.leibie,self.tname];
    
    
    
       NSString *strTrue = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [SVProgressHUD show];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager GET:strTrue parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SVProgressHUD dismiss];

        NSDictionary *dic = responseObject;
        NSMutableArray *arry = [dic objectForKey:@"list"];
        for (NSDictionary *dicList in arry) {
            HotSecond *SecondHot = [[HotSecond alloc] init];
            [SecondHot setValuesForKeysWithDictionary:dicList];
            [self.arr addObject:SecondHot];
        }
        
        [self.tableview reloadData];
        //[self.tableview headerBeginRefreshing];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      //  NSLog(@"error: %@", error);
    }];
    
}
- (void)CreateTableViewShow
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = [UIColor colorWithRed:225/255.0 green:237/255.0 blue:238/255.0 alpha:1];
    [self.view addSubview:_tableview];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"hotSecond";
    HotSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[HotSecondCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
   
    HotSecond *SecondHot = [self.arr objectAtIndex:indexPath.row];
    cell.SecondHot = SecondHot;
      return cell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
